<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>正则表达式替换</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.ereg-replace" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ereg_replace</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">ereg_replace</span> &mdash; <span class="dc-title">正则表达式替换</span></p>

 </div>
 <div class="refsect1 unknown-seealsp" id="refsect1-function.ereg-replace-unknown-seealsp">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>ereg_replace</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$replacement</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>
   ) : <span class="type">string</span></div>

  <p class="simpara">
   本函数在 <code class="parameter">string</code> 中扫描与
   <code class="parameter">pattern</code> 匹配的部分，并将其替换为
   <code class="parameter">replacement</code>。
  </p>
  <p class="simpara">
   返回替换后的字符串。（如果没有可供替换的匹配项则会返回原字符串。）
  </p>
  <p class="simpara">
   如果 <code class="parameter">pattern</code>
   包含有括号内的子串，则 <code class="parameter">replacement</code>
   可以包含形如
   <em>\\<span class="replaceable">digit</span></em>
   的子串，这些子串将被替换为数字表示的的第几个括号内的子串；<em>\\0</em>
   则包含了字符串的整个内容。最多可以用九个子串。括号可以嵌套，此情形下以左圆括号来计算顺序。
  </p>
  <p class="simpara">
   如果未在 <code class="parameter">string</code>
   中找到匹配项，则
   <code class="parameter">string</code> 将原样返回。
  </p>
  <p class="para">
   例如，下面的代码片断输出
   &quot;This was a test&quot; 三次：
  </p>
  <p class="para">
   <div class="example" id="example-5881">
    <p><strong>Example #1 <span class="function"><strong>ereg_replace()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;a&nbsp;test"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"&nbsp;is"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"&nbsp;was"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"(&nbsp;)is"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\\1was"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"((&nbsp;)is)"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\\2was"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   要注意的一点是如果在 <code class="parameter">replacement</code>
   参数中使用了整数值，则可能得不到所期望的结果。这是因为
   <span class="function"><strong>ereg_replace()</strong></span> 将把数字作为字符的序列值来解释并应用之。例如：
  </p>
  <p class="para">
   <div class="example" id="example-5882">
    <p><strong>Example #2 <span class="function"><strong>ereg_replace()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;不能产生出期望的结果&nbsp;*/<br /></span><span style="color: #0000BB">$num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This&nbsp;string&nbsp;has&nbsp;four&nbsp;words."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'four'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Output:&nbsp;'This&nbsp;string&nbsp;has&nbsp;&nbsp;&nbsp;words.'&nbsp;*/<br /><br />/*&nbsp;本例工作正常&nbsp;*/<br /></span><span style="color: #0000BB">$num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'4'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This&nbsp;string&nbsp;has&nbsp;four&nbsp;words."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'four'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Output:&nbsp;'This&nbsp;string&nbsp;has&nbsp;4&nbsp;words.'&nbsp;*/<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5883">
    <p><strong>Example #3 将 URL 替换为超连接</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ereg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"[[:alpha:]]+://[^&lt;&gt;[:space:]]+[[:alnum:]/]"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"&lt;a&nbsp;href=\"\\0\"&gt;\\0&lt;/a&gt;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <div class="tip"><strong class="tip">Tip</strong>
   <p class="para">
    <span class="function"><a href="preg_replace.html" class="function">preg_replace()</a></span> 函数使用了
    Perl 兼容正则表达式语法，通常是比
    <span class="function"><strong>ereg_replace()</strong></span> 更快的替代方案。
   </p>
  </div>
  <p class="para">
   参见 <span class="function"><a href="ereg.html" class="function">ereg()</a></span>，<span class="function"><a href="eregi.html" class="function">eregi()</a></span>，<span class="function"><a href="eregi_replace.html" class="function">eregi_replace()</a></span>，<span class="function"><a href="str_replace.html" class="function">str_replace()</a></span>
   和 <span class="function"><a href="preg_match.html" class="function">preg_match()</a></span>。
  </p>
 </div>

</div></div></div></body></html>